package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.common.CategoryException;
import com.itheima.common.R;
import com.itheima.dao.OrdersDao;
import com.itheima.domain.*;
import com.itheima.service.*;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements OrdersService {

    @Autowired
   private OrderDetailService orderDetailService;

   @Autowired
   private ShoppingCartService shoppingCartService;

   @Autowired
   private AddressBookService addressBookService;

   @Autowired
   private UserService userService;



   //下单功能
    @Override
    @Transactional
    public void submit(Orders orders) {

        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();

        //查询用户当前购物车数据
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        if (list==null ||list.size()==0){
            throw new CategoryException("购物车为空，不能下单");
        }
        AtomicInteger atomicInteger = new AtomicInteger(0);
        //遍历购物数据并计算总金额
        List<OrderDetail> orderDetaillist=list.stream().map((item)->{

            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(currentId);
            orderDetail.setNumber(item.getNumber());
            orderDetail.setImage(item.getImage());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setAmount(item.getAmount());
            atomicInteger.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;

        }).collect(Collectors.toList());
       //查询当前用户地址数据
        AddressBook byId1 = addressBookService.getById(orders.getAddressBookId());
        //查询当前用户信息
        User byId = userService.getById(currentId);

        //添加数据
        long id = IdWorker.getId();

        orders.setNumber(String.valueOf(id));
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setUserId(currentId);
//        orders.setId(currentId);
        orders.setUserName(byId.getName());
        orders.setConsignee(byId1.getConsignee());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPhone(byId.getPhone());
        orders.setAddress((byId1.getProvinceName()==null? "": byId1.getProvinceName())
                +(byId1.getCityName() ==null? "": byId1.getCityName())
                +(byId1.getDistrictName()==null? "":byId1.getDistrictName())
                 +(byId1.getDetail()==null? "":byId1.getDetail()));
        orders.setAmount(new BigDecimal(atomicInteger.get()));//总金额
        this.save(orders);

        //向订单明细插入数据
      orderDetailService.saveBatch(orderDetaillist);
        //清空当前用户购物车数据

        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);

    }


//

}
